﻿using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using game.character.battle;
using lumo;
using lumo.display.object2D;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using lumo.display.animation2D;

namespace game.character.enemy
{
    /// <summary>
    /// The Fish.
    /// </summary>
    public class EnemyFish : EnemyBase
    {

        protected const string _Texture = "Graphics/Enemies/MutantFish/MutantFishWalking";
        protected const int _Life = 1000;
        protected const float _Hit = 1000;

        /// <summary>
        /// Constructor for RedEnemy.
        /// </summary>
        public EnemyFish(LumoComponent component, GameMap map, Tiled.Object tiledObject)
            : base(component, map, tiledObject)
        {
            Battler = new GameEnemy(_Life);
            Battler.HitRate = _Hit;

            // Save the texture.
            Texture = component.Content.Load<Texture2D>(_Texture);
        }

        /// <summary>
        /// Update the character.
        /// </summary>
        /// <param name="component">Update using lumo</param>
        protected override void CreateBody()
        {
            // Create Submarine Body.
            Body = BodyFactory.CreateRectangle(Global.Map.World, Texture.Width / 4 / Global.PhysicsScale, Texture.Height / Global.PhysicsScale, 0.125f);
            Body.BodyType = BodyType.Dynamic;
            Body.IsSensor = true;
            Body.IgnoreGravity = true;
            Body.CollisionCategories = Global.PhysicsCategory.Enemy;
            Body.CollidesWith = Global.PhysicsCategory.Actor | Global.PhysicsCategory.ActorBullet;
            Body.LinearDamping = 1.0f;
            Body.AngularDamping = 1.0f;
            Body.Restitution = 0.25f;
        }

        /// <summary>
        /// Setup the Parameters
        /// </summary>
        protected override void SetupParameters()
        {
            // Setup The coordinates
            Position        = new Vector2(_TiledObject.X, _TiledObject.Y);
            SourceRectangle = new Rectangle(0, 0, Texture.Width / 4, Texture.Height);
            ScreenOrigin    = new Vector2(Texture.Width / 8, Texture.Height / 2);
        }

        /// <summary>
        /// Setup the Artificial Intelligency.
        /// </summary>
        protected override void SetupAI()
        {
            // Get Properties.
            float Speed = GetFloatProperty("Speed", 100f);
            float PointSize = GetFloatProperty("PointSize", 100f);
            float SightRange = GetFloatProperty("SightRange");

            // Clear the velocity.
            AIClearVelocity();

            // HP Condition.
            AIOnCondition(() => Battler.HP <= (Battler.MaxHP / 4), () => "FishDying");

            //
            AIOnRange(SightRange, _GameMap.Player, () => "PlayerOnRange");
            

            // The route patter of the fish
            if (_TiledObject.GeometryType == Tiled.Object.GeometryTypes.Line)
            {
                AIAction MoveAction = AIFixedMoviment(_TiledObject.GetAbsolutePoints(true), Speed, PointSize);
                // Only When Not On Range of the player
                if (MoveAction != null)
                    MoveAction.OnNotGroups("PlayerOnRange", "FishDying");
            }

            // Follow On Range
            if (SightRange > 0)
            {
                AIAction FollowAction = AIOnRangePattern(SightRange, _GameMap.Player, Speed, AIRangePattern.Follow);
                if (FollowAction != null)
                    FollowAction.OnGroupsSelective(
                        new string[] { "PlayerOnRange" },
                        new string[] { "FishDying" }
                    );
            }

            // Fish flee when dying and on range of the player.
            AIAction FleeAction = AIOnRangePattern(SightRange, _GameMap.Player, Speed/2.0f, AIRangePattern.Flee);
            if (FleeAction != null)
                FleeAction.OnGroups("PlayerOnRange", "FishDying");

            // Turn on AI
            AITurn();

            // Damage the object on contact
            AIDamageOnContact(_GameMap.Player, "Damaging");

            // Animation.
            FrameAnimation2D Animation = new FrameAnimation2D(4, 1, new uint[] { 0, 1, 2, 3, 1, 3, 1, 2, 0, 3 }, 800.0f, Smooth.None, Loop.Reset, this);
            AIOnUpdate(
                () =>
                {
                    Animation.Update(_LumoComponent);
                    return null;
                }
            );

        }

    }

}